package com.example.duomeitijuhe.service;

import android.os.AsyncTask;
import com.example.duomeitijuhe.bean.Comment;
import com.example.duomeitijuhe.bean.UserToken;
import com.example.duomeitijuhe.constant.WeiboConstant;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lx on 2014/10/16.
 */
public class WeiboGetComment extends AsyncTask<UserToken , Void , String> {

    private httpSupport httpSupport ;

    private UserToken userToken ;

    private String weiboId ;

    private List<Comment> comments ;

    public  WeiboGetComment( String weiboId){
        this.weiboId = weiboId ;
    }

    @Override
    protected String doInBackground(UserToken... params) {
        this.userToken = params[0] ;

        String request = WeiboConstant.COMMENTS_SHOW + WeiboConstant.ACCESS_TOKEN + userToken.getUserToken() ;

        this.httpSupport = new httpSupport(request) ;

        return httpSupport.sendRequest();
    }

    @Override
    protected void onPostExecute(String s) {

        comments = new ArrayList<Comment>() ;

        try{

            JSONObject jsonObject = new JSONObject(s) ;
            JSONArray jsonArray = jsonObject.getJSONArray("comments") ;

            for( int i = 0 ; i < jsonArray.length() ; i++){
                JSONObject commentsjson = jsonArray.getJSONObject(i) ;
                Comment comment = new Comment() ;
                comment.setSendTime(commentsjson.getString("created_at"));
                comment.setCommentId(commentsjson.getString("id"));
                comment.setText(commentsjson.getString("text"));

                JSONObject user = commentsjson.getJSONObject("user") ;

                comment.setSendId(user.getString("id"));
                comment.setSendName(user.getString("name"));
                comment.setSendIcon(user.getString("profile_image_url"));
            }


        }catch (JSONException e){
            e.printStackTrace();
        }



        super.onPostExecute(s);
    }
}
